 /*
Purpose: Append round 7 and 8 and make variables comparable with previous dataset format.

 */
    cd "${direc}/data/"

    use "raw/styl_endline_clean_combined.dta", clear
    append using "raw/styl_round2_clean_combined.dta", gen(longrun_r2) force


* Replacing missing values with missing tag
    mvdecode _all, mv(-999 = .n \ -998 = .n \ -888 = .r \ -777 = .d)


*Adding expat and combined surveys, removing refused phone surveys and misidentified case.

*Adding Expat surveys (matches with survey_status_phone variable in styl_outcomes.dta, marked as left Liberia surveyed.)
    replace survey_status_round1 = 1 if id == 30180
    replace survey_status_round1 = 1 if id == 30298
    replace survey_status_round1 = 1 if id == 30313
    replace survey_status_round1 = 1 if id == 30014

*Adding combined surveys
    replace survey_status_round1 = 1 if id == 30015
    replace survey_status_round1 = 1 if id == 30195
    replace survey_status_round1 = 1 if id == 30446
    replace survey_status_round1 = 1 if id == 30465


*Fixing refusal of consent for phone surveys (matches with survey_status_phone variable in styl_outcomes.dta)
    replace consented_endline = 0 if id == 30375
    replace consented_endline = 0 if id == 20381
    replace consented_endline = 0 if id == 20012


*Misidentified case: (not located, not dead not left the country, etc)
    drop if go_ahead == 0      /*  this is to get rid of id 74212456, which was a started interview to the wrong person.  */


*Keeping only consented and full surveys.
    drop if consented_endline==0
    drop if consented_endline_r2==0
    drop if survey_status_round1 == 5
    drop if survey_status_round2 == 5
    drop if survey_status_round1 == 4
    drop if survey_status_round2 == 4
    drop if survey_status_round1 == . &  survey_status_round2 == .



      format  id %12.0g
      tostring id, gen(partid)

    * Dropping enumerator and other survey meta data
//          drop  deviceid subscriberid simid devicephonenum username text_audit audio_audit field_comments sstat_pct_conversation sstat_pct_quiet sstat_pct_quiet ///
//            sstrm_sound_level sstrm_sound_pitch users duration_pts pull_survey_status pub_to_users enum_team enum_name enum_id pilot_id enum_name_text pilot pilot_community community ///
//            pilot_fname pilot_middle_name pilot_mname pilot_lname

    * Dropping the drug specific dummies and steal weapon dummies, distraction during interview dummies
      drop drugntgrssspecify_1 drugntgrssspecify_2 drugntgrssspecify_3 drugntgrssspecify_4 drugntgrssspecify_5 drugntgrssspecify_6 drugntgrssspecify_7 drugntgrssspecify_8 drugntgrssspecify_9 drugntgrssspecify_10 drugntgrssspecify__777 drugntgrssspecify__888 drugntgrssspecify__999 ///
           stealweaponspecify_1 stealweaponspecify_2 stealweaponspecify_3 stealweaponspecify_4 stealweaponspecify_5 stealweaponspecify_6 stealweaponspecify_7 stealweaponspecify_8 stealweaponspecify_9 stealweaponspecify__777 stealweaponspecify__888 stealweaponspecify__999 ///
           ccldistract_0 ccldistract_1 ccldistract_2 ccldistract_3 ccldistract_4 ccldistract_5 ccldistract_6 ccldistract_7 ccldistract_8 ccldistract_9 ccldistract_10 ccldistract__777   econ_shock_unemployed_lengthunit econ_shock_schoolfees_lengthunit econ_shock_housing_lengthunit

    * Dropping badly rescaled variables
  drop  baddo_resc badhear_resc badsad_resc badtired_resc badeat_resc badimportant_resc badtalk_resc badfrust_resc badspoiled_resc badspirit_resc baddry_resc badsat_resc ///
                     badburn_resc nvstorekeeperpunish_resc nvkillstorekeeperpunish_resc attviolence depression

  //    keep if which_round == "first round"

     foreach v of varlist ladderrich ladderresp ladderpower ladderaccess ladderrich1yr ladderresp1yr ladderpower1yr ladderaccess1yr ladder1yrago laddernow ladder1yr ladder5yr  happiness satisfaction health ///
     time_decl_patienceladder {
       decode `v', gen(`v'_s)
          destring `v'_s, replace
        drop `v'
       rename `v'_s `v'
      }

    label define ladder 1"1" 2"2" 3"3" 4"4" 5"5" 6"6" 7"7" 8"8" 9"9" 10"10"
    label value  ladder10yrago ladder5yrago  ladder

* ea stands for economic activity
* ea1 has the value label of the first reported activity because people report multiple activities.
* for example, ea9 onwards doesn't exist as nobody reports more than 8 activities.
* storing it into ea categories

destring ea_count, replace
qui summ ea_count
local max_activities = r(max)

* iterating over all economic activities

forvalue i= 1/`max_activities' {
gen ea_cat_`i' = .
replace ea_cat_`i' = 1  if ea`i' =="selling from shop/container/store"
replace ea_cat_`i' = 2  if ea`i' =="selling from a market table or booth"
replace ea_cat_`i' = 3  if ea`i' =="other buying and selling around business (i.e. wheelbarrow, water, etc)"
replace ea_cat_`i' = 4  if ea`i' =="any other kind of small small business"
replace ea_cat_`i' = 5  if ea`i' =="a skilled trade or vocation"
replace ea_cat_`i' = 6  if ea`i' =="office work (like NGO, government or teaching work)"
replace ea_cat_`i' = 7  if ea`i' =="carloading"
replace ea_cat_`i' = 8  if ea`i' =="car washing"
replace ea_cat_`i' = 9  if ea`i' =="peim-peim riding"
replace ea_cat_`i' = 10 if ea`i' =="carrying any load"
replace ea_cat_`i' = 11 if ea`i' =="watchman job"
replace ea_cat_`i' = 12 if ea`i' =="house boy work, brushing, cleaning, or sweeping, collecting garbage, beating blocks, or unskilled construction work"
replace ea_cat_`i' = 13 if ea`i' =="looking for scrap"
replace ea_cat_`i' = 14 if ea`i' =="begging for money from people you don't know"
replace ea_cat_`i' = 15 if ea`i' =="farming"
replace ea_cat_`i' = 16 if ea`i' =="selling opium or drugs (not tablets)"

replace ea_cat_`i' = 17 if ea`i' =="any other kind of hustling"
replace ea_cat_`i' = 17 if ea`i' =="some other kind of hustling"

replace ea_cat_`i' = 18 if ea`i' =="something else to get money"
replace ea_cat_`i' = 18 if ea`i' =="anything else to get money"

replace ea_cat_`i' = 19 if ea`i' =="nothing to earn money"
replace ea_cat_`i' = 19 if ea`i' =="nothing to earn money [DON'T READ]"

}

local nshop        =1
local ntable       =2
local nbuysell     =3
local npettytrade  =4     // te review. This category should be small biz
local ntrade       =5
local noffice      =6
local ncarload     =7
local ncarwash     =8
local nmoto        =9
local ncarryload   =10
local nguard       =11
local nhouseclswp  =12
local nscavenge    =13
local nbeg         =14
local nfarm        =15
local ndrugsell    =16
local nworkoth1    =17
local nworkoth2    =18

* Sahil's tag: added pettytrade in this loop on 28th April 2023
foreach x in shop table buysell  trade office carload carwash moto carryload guard houseclswp scavenge beg farm drugsell   workoth1 workoth2 {

 gen `x'2wowner = .
 gen `x'7ddys = .
 gen `x'7dhrs = .
 gen `x'prev7ddys = .
 gen `x'prev7dhrs = .
 gen `x'7dprof = .
 gen `x'7dprofunit = .
 gen `x'prev7dprof = .
 gen `x'prev7dprofunit = .
 gen `x'rawmatval = .
 gen `x'rawmatvalunit = .
 gen `x'equipval = .
 gen `x'equipvalunit = .
 gen `x'structval = .
 gen `x'structvalunit = .
 gen `x'mercsval = .
 gen `x'mercsvalunit = .

* iterating over the ea label and storing it into labelled activity type. there are 18 activity types and 8 maximum activities in the local macro `max_activities'

   forvalue i= 1/`max_activities' {
      replace  `x'2wowner         = ea2wowner_`i'         if ea_cat_`i'==`n`x''
      replace  `x'7ddys           = ea7ddys_`i'           if ea_cat_`i'==`n`x''
      replace  `x'7dhrs           = ea7dhrs_`i'           if ea_cat_`i'==`n`x''
      replace  `x'prev7ddys       = eaprev7ddys_`i'       if ea_cat_`i'==`n`x''
      replace  `x'prev7dhrs       = eaprev7dhrs_`i'       if ea_cat_`i'==`n`x''
      replace  `x'7dprof          = ea7dprof_`i'          if ea_cat_`i'==`n`x''
      replace  `x'7dprofunit      = ea7dprofunit_`i'      if ea_cat_`i'==`n`x''
      replace  `x'prev7dprof      = eaprev7dprof_`i'      if ea_cat_`i'==`n`x''
      replace  `x'prev7dprofunit  = eaprev7dprofunit_`i'  if ea_cat_`i'==`n`x''
      replace  `x'rawmatval       = earawmatval_`i'       if ea_cat_`i'==`n`x''
      replace  `x'rawmatvalunit   = earawmatvalunit_`i'   if ea_cat_`i'==`n`x''
      replace  `x'equipval        = eaequipval_`i'        if ea_cat_`i'==`n`x''
      replace  `x'equipvalunit    = eaequipvalunit_`i'    if ea_cat_`i'==`n`x''
      replace  `x'structval       = eastructval_`i'       if ea_cat_`i'==`n`x''
      replace  `x'structvalunit   = eastructvalunit_`i'   if ea_cat_`i'==`n`x''
      replace  `x'mercsval         = eamercval_`i'         if ea_cat_`i'==`n`x''
      replace  `x'mercsvalunit     = eamercvalunit_`i'     if ea_cat_`i'==`n`x''

}
 tostring `x'2wowner , replace
 replace  `x'2wowner = "1-Owner" if `x'2wowner =="1"
 replace  `x'2wowner = "2-Employee" if `x'2wowner =="0"


}
 * -777 is a fake value used by enumerator to specify 'other'

      recode carryweaponkind (-777=96)

      tostring  mainactivity, replace

      tostring handoutstuff handouttrain, replace force
      replace handoutstuff = subinstr(handoutstuff,"1","1-Food",.)
      replace handoutstuff = subinstr(handoutstuff,"2","2-Goods",.)
      replace handoutstuff = subinstr(handoutstuff,"3","3-Tools",.)
      replace handoutstuff = subinstr(handoutstuff,"4","4-Nothing",.)
       replace handoutstuff = subinstr(handoutstuff," ",";",.)


      replace handouttrain = subinstr(handouttrain,"1","1-Training",.)
      replace handouttrain = subinstr(handouttrain,"2","2-Counselling",.)
      replace handouttrain = subinstr(handouttrain,"3","3-Medical\Assistance",.)
      replace handouttrain = subinstr(handouttrain,"4","4-Nothing",.)
        replace handouttrain = subinstr(handouttrain," ",";",.)
        replace handouttrain = subinstr(handouttrain,"\"," ",.)

/*
      replace disabledspecify = subinstr(disabledspecify,"1","1-" ,.)
      replace disabledspecify = subinstr(disabledspecify,"2","1-" ,.)
      replace disabledspecify = subinstr(disabledspecify,"3","1-" ,.)
      replace disabledspecify = subinstr(disabledspecify,"4","1-" ,.)
      replace disabledspecify = subinstr(disabledspecify,"5","1-" ,.)
      replace disabledspecify = subinstr(disabledspecify,"6","1-" ,.)
      replace disabledspecify = subinstr(disabledspecify,"7","1-" ,.)
      replace disabledspecify = subinstr(disabledspecify,"7","1-" ,.)
*/

     * Adding the names of drugs to the value
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"1","1-Diazepan",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"2","2-Bubble\or\10-10\or\Valium",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"3","3-Italian\White",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"4","4-Tie\White",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"5","5-Rolling\Tide",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"6","6-Brown\brown",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"7","7-Dugee",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"8","8-Cocaine",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"9","9-Heroin",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"10","10-Tramadoze",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"-777","96-Other",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"-888","98-Refuse\to\answer",.)
      replace drugntgrssspecify = subinstr(drugntgrssspecify,"-999","98-Refuse\to\answer",.)
        replace drugntgrssspecify = subinstr(drugntgrssspecify," ",";",.)
        replace drugntgrssspecify = subinstr(drugntgrssspecify,"\"," ",.)

      replace stealweaponspecify = subinstr(stealweaponspecify,"1","1-Knife",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"2","2-Cutlass/Machete",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"3","3-False\gun",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"4","4-Gun",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"5","5-Pepper\Spray",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"6","6-Razorblade",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"7","7-Acid\water",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"8","8-Screwdriver",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"9","9-Scissors",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"10","10-Other",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"-777","96-Other",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"-888","98-Refuse\to\answer",.)
      replace stealweaponspecify = subinstr(stealweaponspecify,"-999","98-Refuse\to\answer",.)
        replace stealweaponspecify = subinstr(stealweaponspecify," ",";",.)
        replace stealweaponspecify = subinstr(stealweaponspecify,"\"," ",.)



      replace ccldistract = subinstr(ccldistract,"0","0-None",.)
      replace ccldistract = subinstr(ccldistract,"1","1-Rain",.)
      replace ccldistract = subinstr(ccldistract,"2","2-Child\present",.)
      replace ccldistract = subinstr(ccldistract,"3","3-Spouse/partner\present",.)
      replace ccldistract = subinstr(ccldistract,"4","4-Friend\present",.)
      replace ccldistract = subinstr(ccldistract,"5","5-Bossman\present",.)
      replace ccldistract = subinstr(ccldistract,"6","6-Others\present",.)
      replace ccldistract = subinstr(ccldistract,"7","7-Had\to\work",.)
      replace ccldistract = subinstr(ccldistract,"8","8-Had\to\eat",.)
      replace ccldistract = subinstr(ccldistract,"9","9-Enumerator\had\to\stop",.)
      replace ccldistract = subinstr(ccldistract,"10","10-Could\hear\other\surveyors",.)
      replace ccldistract = subinstr(ccldistract,"-777","96-Other ",.)
        replace ccldistract = subinstr(ccldistract," ",";",.)
        replace ccldistract = subinstr(ccldistract,"\"," ",.)



rename trustleader trusttleader

  ds _all , has(vall)
       local tostringval `r(varlist)'

      numlabel , add mask("#-")

      foreach var in `tostringval'{
        decode `var', generate(`var'_str)
        drop `var'
        rename `var'_str `var'
      }

          // MAKE ALL VARIABLES STRING (To check later)
       tostring *, replace force




save "clean/STYL_10yrs_clean.dta", replace
cd $direc
